package com.github.postsanf.yinian.timeline;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.YNTimeLineActivity;


public class FirstFragment extends BaseTimelineFragment {
    private View mView;
    private int[] mAnimationViewIds = {
            R.id.iv_timeline_ball1, R.id.iv_timeline_ball2,
            R.id.iv_timeline_ball3,R.id.iv_timeline_ball4,R.id.rl_timeline_start
    };

    private Handler handler = new Handler();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        mView =inflater.inflate(R.layout.timeline_first, container, false);
        handler.postDelayed(mRunnableBall, 1000);
        initView();
        return  mView;
    }

    private void initView(){
        // 点击开启忆年时光记忆
        mView.findViewById(mAnimationViewIds[4]).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((YNTimeLineActivity)getActivity()).switchPage(1);
            }
        });
    }

    private  Runnable  mRunnableBall= new Runnable() {
        @Override
        public void run() {
            doBallAnim();
        }
    };

    private void doBallAnim(){
        try{
            mView.findViewById(mAnimationViewIds[0]).startAnimation(getBallAnimation(R.anim.anim_stars_rotate));
            mView.findViewById(mAnimationViewIds[1]).startAnimation(getBallAnimation(R.anim.anim_stars_ball2));
            mView.findViewById(mAnimationViewIds[2]).startAnimation(getBallAnimation(R.anim.anim_stars_ball3));
            mView.findViewById(mAnimationViewIds[3]).startAnimation(getBallAnimation(R.anim.anim_stars_ball4));
            mView.findViewById(mAnimationViewIds[4]).startAnimation(getBallAnimation(R.anim.anim_stars_start));
        }catch (NullPointerException e){}
    }
    private Animation getBallAnimation(int resID){
        Animation animation = AnimationUtils.loadAnimation(getActivity(),resID);
        return animation;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
//        for (int i = 0; i < mAnimationViewIds.length; i++) {
//            Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.anim_stars_bg1);
//            animation.setDuration(ANIMATION_DURATION);
//            animation.setStartOffset(ANIMATION_OFFSET * i);
//            view.findViewById(mAnimationViewIds[i]).startAnimation(animation);
//        }
    }

    @Override
    public int[] getChildViewIds() {
        return new int[]{
                R.id.iv_timeline_ball1,R.id.iv_timeline_ball2,
                R.id.iv_timeline_ball3,R.id.iv_timeline_ball4,R.id.rl_timeline_start
        };
    }

    @Override
    public int getRootViewId() {
        return R.id.root_timeline_first;
    }
}
